import org.jsoup.Connection;
import org.jsoup.HttpStatusException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Picture {
    static Init init = new Init();
    static Scanner sc = new Scanner(System.in);
    static void Baidu() throws IOException {
        System.out.println("目标百度,全力爬取");
        init.initUnSecureTSL();
        System.out.println("请输入要搜索的内容:");
        String urls=java.net.URLEncoder.encode(sc.next(), StandardCharsets.UTF_8);
        Connection connect = Jsoup.connect("https://image.baidu.com/search/index?tn=baiduimage&word="+urls);
        Connection conheader;
        int i = Init.userAgents.size();
        Document document = null;
        HashMap<String,String> header = null;
        while(true){
            if (Init.userAgents.isEmpty()){
                System.out.println(i+"条用户标识已耗尽");
                break;
            }
            try{
                header=init.Builder(Main.urls[0]);
                conheader = connect.headers(header);
                document=conheader.get();
            }catch(HttpStatusException e){
                System.out.println("用户标识失效,重新更换标识");
                continue;
            }
            System.out.println("已成功连接百度");
            break;
        }
        ArrayList<String> list = new ArrayList<>();
        String[] str = document.html().split("objURL");
        for (int j = 1; j<str.length;j++){
            String s = str[j].split(",")[0];
            list.add(s.substring(3,s.length()-1));
        }
        Thread thread = new Thread(new Baidu_pic(list,header));
        thread.start();
    }
}
